/************** init.html **************/
function query_topic(page) {
	var parm = $('#queryForm').serialize() + (page != null ? ('&page=' + page) : '');
	var url = create_url('topic-query');
	sys_record_parm('topic',parm);
	sys_loading();
	$.ajax({type:'POST', url:url, data:parm,
		success: function(result) {
			sys_loading_close();
			$('#resultList').html(result);
		}
	});
}

/********************list.html***************************/
function delete_topic(code, name){
	var submit = function (v, h, f) {
      if (v == true){
      	var url = create_url('topic-delete');
      	aidExecute_topic(code, url);
      }
      return true;
  };
  $.Zebra_Dialog("确认删除题干为 " + name + " 的信息吗？", "提示", submit, {buttons: {'是': true, '否': false }});
}

function aidExecute_topic(code, url) {
	var parm = {'code':code};
	sys_submiting();
	$.ajax({type:'POST', url:url, data:parm,
		success: function(result) {
			sys_submiting_close();
			if ('success' == result) {
				sys_msg('操作成功');
				query_topic();
			} else {
				sys_msg('操作失败', '操作失败，请刷新页面再次尝试，如果问题依然存在，请联系管理员!');
			}
		}
	});
}
/************** edit.html **************/
/**
 * onchange事件
 */
function showAnswers_topic(){
	var type = $("#tType").val();
	$("#addTbody tr").each(function(){
		$(this).remove();
	});
	$("#theadDiv tr").remove();
	addThead_topic();
	if(type == "1" || type == "2"){
		for(var i = 0 ; i < 4 ; i++){
			addRow_topic();
		}
	} else if(type == "3"){
		addRow_topic();
	} else{
		$("#theadDiv tr").remove();
	}
}
/**
 * 添加TABLE头部
 */
function addThead_topic(){
	var type = $("#tType").val();
	var thead = '';
	if(type == "1" || type == "2"){
		thead = $("#addTable").find("tr.thead1").clone(true);
		$("#theadDiv").append(thead);
	} else {
		thead = $("#addTable").find("tr.thead2").clone(true);
		$("#theadDiv").append(thead);
	}
}
/**
 * 添加行
 */
function addRow_topic(){
	var index = parseInt($("#index").val());
	var type = $("#tType").val();
	var newTr = '';
	if(type == "1"){
		newTr = $("#addTable").find("tr.addrad").clone(true);
		newTr.find("td:eq(0) > input:text:eq(0)").attr("name","itemList[" + index + "].answer").end()
		.find(".ro").attr("name","itemList[" + index + "].isRight");
	} else if (type == "2"){
		newTr = $("#addTable").find("tr.addche").clone(true);
		newTr.find("td:eq(0) > input:text:eq(0)").attr("name","itemList[" + index + "].answer").end()
		.find(".cx").attr("name","itemList[" + index + "].isRight");
	} else {
		newTr = $("#addTable").find("tr.addjd").clone(true);
		newTr.find(".jd").attr("name","itemList[" + index + "].answer").end()
		.find(".da").attr("name","itemList[" + index + "].isRight");
	}
	$("#addTbody").append(newTr);
	index = index + 1;
	$("#index").val(index);
}

function deleteRow_topic(btn){
	btn.parent().parent().remove();
}

/**
 * 控制值只能选中已个单选按钮
 */
function onlyRadio_topic(btn){
	$("input:radio").each(function(){
		$(this).removeAttr("checked");
	});
	btn.attr("checked",true);
}

/**
 * 选择课程
 */
function chooseCourse_topic(){
	var codes = $("#codes").val();
	var url = create_url('topic-aidInit');
	var parm = {"codes":codes};
	$.colorbox({overlayClose:false, href:url, data:parm, close:'[关闭]', width:'850px', height:'600px', open:true,
		onClosed:function(){
		}
	});
}

function aidqueryCourse_topic(page) {
	var parm = $('#queryCourseForm1').serialize() + sys_get_page(page);
	var url = create_url('topic-aidList');
	sys_loading();
	$.ajax({type:'POST', url:url, data:parm,
		success: function(result) {
			sys_loading_close();
			$('#courseResultList').html(result);
		}
	});
}

function  confirmCourse_topic(){
	$("#courseTbody tr").each(function(){
		$(this).remove();
	})
	$("#addTbody2 tr").each(function(){
		var name = $(this).find(".cName").val();
		var code = $(this).find(".cCode").val();
		var newTr = $("#courseTable").find("tr").clone(true);
			newTr.find("td:eq(0)").text(name).end()
			.find(".yxCode").val(code);
			$("#courseTbody").append(newTr);
	})
	//把CODES值附到 EIDT上面
	var codes = "";
	$(".cCode").each(function(){
		if($(this).val().length != 0){
			codes = $(this).val() + ","+ codes  ;
		}
	})
	$("#codes").val(codes);
	if(codes == ""){
		$("#courseDiv").hide();
	} else {
		$("#courseDiv").show();
	}
	$.fn.colorbox.close();
}
/*********************aidList***********************/
function choiseCourse_topic(btn,code,name){
	btn.parent().parent().remove();
	var newTr = $("#tjTable").find(".tr1").clone(true);
		newTr.find("td:eq(0)").text(name).end()
		.find(".cName").val(name).end()
		.find(".cCode").val(code);
	$("#addTbody2").append(newTr);
	var codes = "";
	$(".cCode").each(function(){
		if($(this).val().length != 0){
			codes = $(this).val() + "," + codes;
		}
	});
	$("#codes1").val(codes);
}

function deleteChoiseCourse_topic(btn){
	btn.parent().parent().remove();
	var codes = "";
	$(".cCode").each(function(){
		if($(this).val().length != 0){
			codes = $(this).val() + "," + codes ;
		}
	})
	$("#codes1").val(codes);
	aidqueryCourse_topic();
}

/**
 * 删除已选中课程
 */
function deletedCourses_topic(btn){
	btn.parent().parent().remove();
	var codes = "";
	$(".yxCode").each(function(){
		if($(this).val().length != 0 ){
			codes = $(this).val() + "," + codes;
		}
	})
	$("#codes").val(codes);
}
/**
 * 保存验证
 */
function checkSave_topic(){
	var codes = $("#codes").val();
	var content = $("#content").val();
	var level = $("#level").val();
	var tType = $("#tType").val();
	if(codes.length == 0){
		sys_msg("提示：请选择该题目属于的课程");
		return;
	}
	if(content.length == 0){
		sys_msg("提示：请填写题干！");
		$("#content").focus();
		return;
	}
	if(level.length == 0){
		sys_msg("提示：请选择题目难度");
		return;
	}
	if(tType.length == 0){
		sys_msg("提示：请选择类型");
		return;
	}
	var flag = false;
	if(tType == "1"){
		
		$("input:radio").each(function(){
			if($(this).attr("checked") == undefined){
				return true;
			}else{
				flag = true;
				return false;
			}
		});
		if(flag == false){
			sys_msg("提示：必须选择一个正确答案");
			return;
		}
	}else if(tType == "2"){
		$("input:checkbox").each(function(){
			if($(this).attr("checked") == undefined){
				return true;
			}else{
				flag = true;
				return false;
			}
		});
		if(flag == false){
			sys_msg("提示：必须选择一个正确答案");
			return;
		}
	}else{
		$(".jd").each(function(){
			if($(this).val().length != 0){
				flag = true;
				return false;
			}
		});
		if(flag == false){
			sys_msg("提示：必须填写参考答案！");
			return;
		}
	}
	save_topic();
}
/**
 * 保存
 */
function save_topic(){
	var codes = "";
	$(".yxCode").each(function(){
		if($(this).val().length != 0 ){
			codes = $(this).val() + "," + codes;
		}
	})
	$("#codes").val(codes);
	console.log(codes);
	var parm = $("#editTopicFrom").serialize();
	var url = create_url('topic-save');
	sys_submiting();
	$.ajax({type:'POST', url:url, data:parm,
		success: function(result) {
			sys_submiting_close();
			if (result == 'success') {
				sys_msg('操作成功');
				sys_load_handle('topic');
			} else {
				sys_msg('操作失败，请刷新后重试');
			}
		}
	});
}

function calcCodes_topic(){
	var codes = "";
	$(".yxCode").each(function(){
		if($(this).val().length != 0 ){
			codes = $(this).val() + "," + codes;
		}
	})
	$("#codes").val(codes);
}